package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.CompanyAttendance;
import com.hengxinguotong.zhihuichengjian.treeview.RecyclerAdapter;
import com.hengxinguotong.zhihuichengjian.utils.DensityUtil;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/4/4
 *
 * 考勤报表-公司考勤排名
 */

public class AttendanceCompanyRankingAdapter extends RecyclerAdapter<CompanyAttendance, AttendanceCompanyRankingAdapter.AttendanceCompanyRankingViewHolder> {
    private int screenWidth;

    public AttendanceCompanyRankingAdapter(Context context, List<CompanyAttendance> dataList) {
        super(context, dataList);
    }

    public void setScreenWidth(int screenWidth) {
        this.screenWidth = screenWidth;
    }

    @Override
    public AttendanceCompanyRankingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_attendance_company_ranking, null);
        return new AttendanceCompanyRankingViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AttendanceCompanyRankingViewHolder holder, int position) {
        holder.bindData(position);
    }

    public class AttendanceCompanyRankingViewHolder extends RecyclerAdapter.BaseViewHolder<CompanyAttendance> {
        @BindView(R.id.company_name)
        HXTextView companyName;
        @BindView(R.id.company_attendance_rate)
        ProgressBar companyAttendanceRate;
        @BindView(R.id.company_attendance_rate_tv)
        HXTextView companyAttendanceRateTv;

        public AttendanceCompanyRankingViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void bindData(int position) {
            CompanyAttendance companyAttendance = dataList.get(position);
            companyName.setText(companyAttendance.getName());
            companyAttendanceRateTv.setText(companyAttendance.getPercent() + "%");
            float percent = Float.parseFloat(companyAttendance.getPercent());
            companyAttendanceRate.setProgress((int) percent);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) companyAttendanceRateTv.getLayoutParams();
            int rightScreen = (int) ((screenWidth - DensityUtil.dip2px(context, 30))*(2.5/3.5) - DensityUtil.dip2px(context, 60));
            int leftMargin = (int) (rightScreen*(percent/100));
            params.setMargins(leftMargin, 0, 0, 0);
            companyAttendanceRateTv.setLayoutParams(params);
        }
    }
}
